
package org.ruiyichen.guizhou.agr.base;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;

import org.ruiyichen.guizhou.agr.utils.L;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * activity 栈管理
 *
 * @author wu_tian
 */
public class ActivityTack {

    public interface Action {
        /**
         * 跳转兑换
         */
        int START_EXCHANGE = 0;
    }


    private final String TAG = ActivityTack.class.getSimpleName();

    private final SparseArray<Class> actionArray = new SparseArray<>();

    public LinkedList<Activity> activityList = new LinkedList<>();

    public static ActivityTack tack = new ActivityTack();

    public int singleTaskkCount = 0;

    public static ActivityTack getInstanse() {
        return tack;
    }

    private ActivityTack() {

    }

    public void addActivity(Activity activity) {
        activityList.add(activity);
        System.out.println("添加:" + activityList.toString());
    }

    public void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    /**
     * 获取当前的Activity
     *
     * @return 当前Activity
     */
    public Activity currentActivity() {
        Activity activity = activityList.get(activityList.size() - 1);
        activityList.getLast();
        Log.d(TAG, (activity != null ? activity.getLocalClassName() : activity) + "");
        return activity;
    }


    /**
     * 获取当前页面上个页面
     * @return
     */
    public Activity getPreviousActivity() {
        if (activityList.size() - 1 > 0) {
            return activityList.get(activityList.size() - 2);
        }
        return null;
    }


    /**
     * 完全退出
     *
     * @param context
     */
    public void finshAllOther(Context context) {
        // int i = activityList.size()-1;
        // while (activityList.size() > 0)
        // {
        // Activity activity = activityList.get(i);
        // if (!activity.getClass().equals(SportIndexActivity.class))
        // {
        // activity.finish();
        // }
        // i--;
        // }

        while (activityList.size() > 0) {
            activityList.get(activityList.size() - 1).finish();
        }
        L.d("删除后:" + activityList.toString());
    }

    public void exit(Context context) {
        while (activityList.size() > 0) {
            activityList.get(activityList.size() - 1).finish();
        }
//        ActivityManager activityManager = (ActivityManager) context
//                .getSystemService(Context.ACTIVITY_SERVICE);
//        activityManager.restartPackage(context.getPackageName());
        System.exit(0);
    }

    /**
     * 根据class name获取activity
     *
     * @param name
     * @return
     */
    public Activity getActivityByClassName(String name) {
        for (Activity ac : activityList) {
            if (ac.getClass().getName().indexOf(name) >= 0) {
                return ac;
            }
        }
        return null;
    }

    @SuppressWarnings("rawtypes")
    public Activity getActivityByClass(Class cs) {
        for (Activity ac : activityList) {
            if (ac.getClass().equals(cs)) {
                return ac;
            }
        }
        return null;
    }

    @SuppressWarnings("rawtypes")
    public void finishActivityByClass(Class cs) {
        Activity ac = getActivityByClass(cs);
        if (ac != null)
            popActivity(ac);
    }

    /**
     * 弹出activity
     *
     * @param activity
     */
    public void popActivity(Activity activity) {
        removeActivity(activity);
        activity.finish();
    }

    public void topActivity(Activity activity) {
        if (activityList.contains(activity)) {
            activityList.remove(activity);
        }

        activityList.add(activity);
        System.out.println("顶点:" + activityList.toString());
    }

    /**
     * 弹出activity到
     *
     * @param cs
     */
    @SuppressWarnings("rawtypes")
    public void popUntilActivity(Class... cs) {
        List<Activity> list = new ArrayList<Activity>();
        for (int i = activityList.size() - 1; i >= 0; i--) {
            Activity ac = activityList.get(i);
            boolean isTop = false;
            for (int j = 0; j < cs.length; j++) {
                if (ac.getClass().equals(cs[j])) {
                    isTop = true;
                    break;
                }
            }
            if (!isTop) {
                list.add(ac);
            } else
                break;
        }
        for (Iterator<Activity> iterator = list.iterator(); iterator.hasNext(); ) {
            Activity activity = iterator.next();
            popActivity(activity);
        }
    }

    public boolean hasStartActivity(Class... clazzs) {
        if (null == clazzs || clazzs.length == 0) return false;
        for (int i = activityList.size() - 1; i >= 0; i--) {
            for (Class clazz : clazzs) {
                if (activityList.get(i).getClass() == clazz) return true;
            }
        }
        return false;
    }

    public int getSingleTaskkCount() {
        return singleTaskkCount;
    }

    public void setSingleTaskkCount() {
        this.singleTaskkCount++;
    }

    /**
     * 设置动作
     *
     * @param action 动作标示位
     * @param clazz  具体动作
     */
    public void setAction(int action, Class clazz) {
        actionArray.put(action, clazz);
    }

    /**
     * 是否有某个动作
     *
     * @param action 动作标示
     * @return true 有
     */
    public boolean hasAction(int action) {
        return actionArray.indexOfKey(action) != -1;
    }

}
